package enstb.web.client;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;




import android.bluetooth.*;
import android.util.Log;
import android.widget.Toast;


public class PresentationService {

	// Unique UUID for this application
	private static final UUID MY_UUID = UUID
			.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");

	private ConnectThread mConnectThread = null;
	private ConnectedThread mConnectedThread=null;
	private BluetoothAdapter mAdapter;

	public PresentationService() {
		mAdapter = BluetoothAdapter.getDefaultAdapter();
	}

	public synchronized void connect(BluetoothDevice device) {

		// Start the thread to connect with the given device
		
		mConnectThread = new ConnectThread(device);
		
		mConnectThread.start();
	}

	private class ConnectThread extends Thread {
		private final BluetoothSocket mmSocket;
		private final BluetoothDevice mmDevice;

		public ConnectThread(BluetoothDevice device) {
			mmDevice = device;
			BluetoothSocket tmp = null;

			// Get a BluetoothSocket for a connection with the
			// given BluetoothDevice
			try {
				Log.d("INFO","get a socket for connection with the fiven bluetooth");
				tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
				Log.d("INFO","creer une connection fini");
			} catch (IOException e) {

			}
			mmSocket = tmp;
		
		}

		public void run() {

			// Make a connection to the BluetoothSocket
			try {
				// This is a blocking call and will only return on a
				// successful connectieon or an exception
				Log.d("INFO","use the socket to etabli une connection");
				mmSocket.connect();
				Log.d("INFO","connection est etabli");
			} catch (IOException e) {
				
			}

			// Reset the ConnectThread because we're done
			synchronized (PresentationService.this) {
				mConnectThread = null;
			}

			// Start the connected thread
			connected(mmSocket, mmDevice);
		}

		public void cancel() {
			try {
				mmSocket.close();
			} catch (IOException e) {
			}
		}
	}

	public synchronized void connected(BluetoothSocket socket,
			BluetoothDevice device) {

		// Cancel the thread that completed the connection
	
		if (mConnectThread != null) {
			mConnectThread.cancel();
			mConnectThread = null;
		}

		// Cancel any thread currently running a connection
		if (mConnectedThread != null) {
			mConnectedThread.cancel();
			mConnectedThread = null;
		}

		

		// Start the thread to manage the connection and perform transmissions
		mConnectedThread = new ConnectedThread(socket);
		Log.d("INFO","les infos socket "+socket.toString());
		Log.d("INFO","les infos de mConnectedTread original "+mConnectedThread.toString());
		mConnectedThread.start();

		
	}
	private class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;

        public ConnectedThread(BluetoothSocket socket) {
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            // Get the BluetoothSocket input and output streams
            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) {
            }
        	
            mmInStream = tmpIn;
            mmOutStream = tmpOut;
            Log.d("INFO","info de outsream quand il est etabli"+mmOutStream.toString());
            String a="left";
            	
			try {
				Log.d("INFO","write in mmOut");
				mmOutStream.write(a.getBytes());
				Log.d("INFO","write fini");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
				
        
        }
        /**
         * Write to the connected OutStream.
         * @param buffer  The bytes to write
         */
        public void write(int n) {
          try {
        	  Log.d("INFO","les infos des sockets write ");  
        	  Log.d("INFO","les infos des mmOutStream dans write"+mmOutStream.toString()); 
        	  mmOutStream.write(n);
        	 

                // Share the sent message back to the UI Activity
            } catch (IOException e) {
              
            }
        }

        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) {
            }
        }
    }

	public void write(int n) {
		ConnectedThread r;
        // Synchronize a copy of the ConnectedThread
        synchronized (this) {
            r = mConnectedThread;
        }
        mConnectedThread.write(n);
        Log.d("INFO","les infos de mConnected dans write "+mConnectedThread.toString());
	}

}
